package yxy.game.pm2.command.pack;

import org.joda.time.DateTime;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_冲榜;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.bean.module.rank.active.RankRushEnum;
import yxy.game.pm2.bean.module.rank.active.RankRushHandle;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.coin.CoinBean;
import yxy.game.pm2.module.coin.CoinModule;
import yxy.game.pm2.module.task.TaskHandle;

/** 背包 兑换钱币 */
@MessageType(proto = Command.CoinConversion, classtype = PackageMessage.CoinConversionRequest.class)
public class CMD_CoinConversion extends Command<PackageMessage.CoinConversionRequest> {

	@Override
	public void execute(MessageSource messageSource, PackageMessage.CoinConversionRequest request) {
		String playerCode = messageSource.getTag();
		int dataId = request.getDataId();

		PackageMessage.CoinConversionResponse.Builder response = PackageMessage.CoinConversionResponse.newBuilder();

		RedisLock lock = CoinModule.lock(playerCode);

		try {
			if (lock.tryLock()) {
				CoinModule coinModule = CoinModule.get(playerCode);
				for (CoinBean bean : coinModule.getCoinBeans()) {
					if (bean.getDataId() == dataId) {
						if (bean.times() <= 0) {
							send(messageSource, response, Result.CoinTimesLack);// 没次数了
							return;
						}
						Currency cost = bean.cost();
						if (cost != null) {
							try {
								new CurrencyHandle().cost(playerCode, CCReason.兑换钱币, cost);
							} catch (CurrencyNotEnoughException e) {
								send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
								return;
							}
						}
						Currency reward = bean.reward(playerCode);
						new CurrencyHandle().addCurrency(playerCode, CCReason.兑换钱币, reward);

						bean.setTimes(bean.getTimes() + 1);
						coinModule.set();

						response.setCoin(new CurrencyHandle().packPoint(reward));
						send(messageSource, response, Result.Success);

						coinModule.checkTimes();

						TaskHandle.handle(playerCode, TaskType.统计型_金币兑换_次数, 1);

						//todo 每次循环的前半小时内不开启榜
						ActiveModule_冲榜 am = ActiveModule_冲榜.get(playerCode, false);
						DateTime begin = am.getRunend().minusDays(6).withHourOfDay(0).withMinuteOfHour(30).withSecondOfMinute(0).withMillisOfSecond(0);
						if (!DateTime.now().isBefore(begin)) {
							RankRushHandle.addRankRushData(playerCode, 1, RankRushEnum.冲榜_金币兑换.getType());
						}

						return;
					}
				}

				send(messageSource, response, Result.ParamError);

			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			lock.unlock();
		}

	}

	private void send(MessageSource messageSource, PackageMessage.CoinConversionResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}